package com.pet.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pet.server.domain.entity.Shop;
import com.pet.server.mapper.ShopMapper;
import com.pet.server.service.IShopService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class ShopServiceImpl implements IShopService {
  private final ShopMapper shopMapper;

  /**
   * 门店申请
   *
   * @param shop
   * @return
   */
  @Override
  public boolean apply(Shop shop) {
    int n = shopMapper.insertOrUpdate(shop);

    return n > 0;
  }

  /**
   * 删除门店
   *
   * @param id
   * @return
   */
  @Override
  public boolean delete(int id) {
    int n = shopMapper.deleteById(id);

    return n > 0;
  }

  /**
   * 查看详情
   *
   * @param id
   * @return
   */
  @Override
  public Shop detail(int id) {
    Shop shop = shopMapper.selectById(id);

    return shop;
  }

  /**
   * 门店修改
   *
   * @param shop
   * @return
   */
  @Override
  public boolean update(Shop shop) {
    int n = shopMapper.insertOrUpdate(shop);

    return n > 0;
  }

  /** 分页查询所有 */
  @Override
  public List<Shop> getAllByPage(int pageNo, int pageSize) {
    Page<Shop> shopPage = shopMapper.selectPage(new Page<>(pageNo, pageSize), null);
    List<Shop> list = shopPage.getRecords();

    return list;
  }

  /**
   * 分页条件查询
   *
   * @param pageNo
   * @param pageSize
   * @param shop
   * @return
   */
  @Override
  public List<Shop> getSomeShopsByPage(int pageNo, int pageSize, Shop shop) {

    // PageModel<Shop> shopPage = shopMapper.selectPage(new PageModel<>(pageNo, pageSize),  new
    // QueryWrapper<>(shop));
    QueryWrapper<Shop> queryWrapper = new QueryWrapper<>();

    if ((shop.getShopName() != null) && !("".equalsIgnoreCase(shop.getShopName()))) {
      queryWrapper.like("shop_name", shop.getShopName());
    }

    if ((shop.getLicenseNumber() != null) && !("".equalsIgnoreCase(shop.getLicenseNumber()))) {
      queryWrapper.like("license_number", shop.getLicenseNumber());
    }

    if ((shop.getShopAddress() != null) && !("".equalsIgnoreCase(shop.getShopAddress()))) {
      queryWrapper.like("address", shop.getShopAddress());
    }

    if ((shop.getShopCorporation() != null) && !("".equalsIgnoreCase(shop.getShopCorporation()))) {
      queryWrapper.like("address", shop.getShopCorporation());
    }

    Page<Shop> shopPage = shopMapper.selectPage(new Page<>(pageNo, pageSize), queryWrapper);
    List<Shop> list = shopPage.getRecords();

    return list;
  }
}

// ~ Formatted by Jindent --- http://www.jindent.com
